#!/bin/sh
function wait_mysql(){
    MYSQL_HOST=${MYSQL_HOST:-""}
    if [ "$MYSQL_HOST" = "" ]; then
        MYSQL_HOST="mysql"
    fi
    MYSQL_PORT=${MYSQL_PORT:-""}
    if [ "$MYSQL_PORT" = "" ]; then
        MYSQL_PORT=3306
    fi
    while [ true ]
    do
        sleep 1
        nc -z -w5 $MYSQL_HOST $MYSQL_PORT
        if [ $? -eq 0 ]; then
            break
        fi
        echo "Waiting for mysql..."
    done
}

if [ "$1" = "sh" ]; then
    exec "sh"
else
    if [ -f /etc/lpszoj.sit ]; then
        echo "[i] Lpszoj start..."
       wait_mysql
    else
        echo "[i] Install Lpszoj... "
        sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
        sed -i 's/#http/http/g' /etc/apk/repositories
        apk update && apk upgrade

        apk --no-cache add tzdata
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
        echo "Asia/Shanghai" > /etc/timezone
        apk --no-cache add php php-common php-gd php-zip php-xml php-simplexml php-mbstring php-fileinfo php-opcache php-fpm php-pdo php-pdo_mysql php-ctype php-session
        apk --no-cache add mysql-dev
        apk --no-cache add git make gcc g++ python3
        cd /app && git clone https://gitee.com/yhssdl/lpszoj.git
        mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.bak
        apk --no-cache add openjdk8

        MYSQL_HOST=${MYSQL_HOST:-""}
        if [ "$MYSQL_HOST" = "" ]; then
            MYSQL_HOST="mysql"
            echo "[i] MySQL host: $MYSQL_HOST"
        fi

        MYSQL_DATABASE=${MYSQL_DATABASE:-""}
        if [ "$MYSQL_DATABASE" = "" ]; then
            MYSQL_DATABASE="ojdate"
            echo "[i] MySQL datebase: $MYSQL_DATABASE"
        fi

        MYSQL_USER=${MYSQL_USER:-""}
        if [ "$MYSQL_USER" = "" ]; then
            MYSQL_USER="ojdate"
            echo "[i] MySQL user: $MYSQL_USER"
        fi

        MYSQL_PASSWORD=${MYSQL_PASSWORD:-""}
        if [ "$MYSQL_PASSWORD" = "" ]; then
            MYSQL_PASSWORD="123456"
            echo "[i] MySQL password: $MYSQL_PASSWORD"
        fi

        MYSQL_PORT=${MYSQL_PORT:-""}
        if [ "$MYSQL_PORT" = "" ]; then
            MYSQL_PORT=3306
            echo "[i] MySQL port: $MYSQL_PORT"
        fi

        wait_mysql

        sed -i "s/mysql:host=.*/mysql:host=$MYSQL_HOST:$MYSQL_PORT;dbname=$MYSQL_DATABASE',/g" /app/lpszoj/config/db.php
        sed -i "s/'username' =>.*/'username' => '$MYSQL_USER',/g" /app/lpszoj/config/db.php
        sed -i "s/'password' =>.*/'password' => '$MYSQL_PASSWORD',/g" /app/lpszoj/config/db.php

        sed -i "s/OJ_HOST_NAME=.*/OJ_HOST_NAME=$MYSQL_HOST/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_USER_NAME=.*/OJ_USER_NAME=$MYSQL_USER/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_PASSWORD=.*/OJ_PASSWORD=$MYSQL_PASSWORD/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_DB_NAME=.*/OJ_DB_NAME=$MYSQL_DATABASE/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_PORT_NUMBER=.*/OJ_PORT_NUMBER=$MYSQL_PORT/g"  /app/lpszoj/judge/config.ini

        sed -i "s/OJ_HOST_NAME=.*/OJ_HOST_NAME=$MYSQL_HOST/g"  /app/lpszoj/polygon/config.ini
        sed -i "s/OJ_USER_NAME=.*/OJ_USER_NAME=$MYSQL_USER/g"  /app/lpszoj/polygon/config.ini
        sed -i "s/OJ_PASSWORD=.*/OJ_PASSWORD=$MYSQL_PASSWORD/g"  /app/lpszoj/polygon/config.ini
        sed -i "s/OJ_DB_NAME=.*/OJ_DB_NAME=$MYSQL_DATABASE/g"  /app/lpszoj/polygon/config.ini
        sed -i "s/OJ_PORT_NUMBER=.*/OJ_PORT_NUMBER=$MYSQL_PORT/g"  /app/lpszoj/polygon/config.ini

        sed -i "s/post_max_size = 8M/post_max_size = 128M/g" /etc/php83/php.ini
        sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 128M/g" /etc/php83/php.ini        

        adduser -D -u 1536 judge
        cd /app/lpszoj
        echo -e "yes" "\n" "admin" "\n" "123456" "\n" "admin@lpszoj.org" | ./yii install
        cd /app/lpszoj/judge
        make
        cd /app/lpszoj/polygon
        make
        echo "lpszoj installation complete." > /etc/lpszoj.sit
    fi
    PHP_VERSION=`php -v>&1|awk 'NR==1{print}'|awk -F ' ' '{print $2}'|awk -F '.' '{printf "%s%s\n", $1, $2}'`
    php-fpm${PHP_VERSION}
    /app/lpszoj/judge/dispatcher -o
    /app/lpszoj/polygon/polygon
    nginx -g "daemon off;"
fi
